//
//  VideoDetailViewController.m
//  WuLi
//
//  Created by Gollum on 16/9/6.
//  Copyright © 2016年 Gollum. All rights reserved.
//

#import "VideoDetailViewController.h"
#import "YYAnimationView.h"
#import "DiscussTableViewCell.h"
#import "MJRefresh.h"
#import "UIImageView+WebCache.h"
#import "NetworkTool.h"
#import "DiscussModel.h"
#import "DiscussData.h"
#import "DiscussAttached.h"

#define kDiscussTableViewCellReuse @"DiscussCell"

@interface VideoDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet UIWebView *webView;


@property (nonatomic,strong)NSMutableArray <DiscussData *>*discussDataArray;
@property (nonatomic,strong)DiscussModel *discussData;


@end

@implementation VideoDetailViewController

- (instancetype)initWithCoder:(NSCoder *)aDecoder{

    self = [super initWithCoder:aDecoder];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 2)];
    
        NSURL *url = [NSURL URLWithString:_urlString];
    NSLog(@"我的字符创%@",_detailString);
        NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:url];
        [_webView loadRequest:urlRequest];
    [self handleDiscussData];
    

    _tableView.rowHeight = 100;
    [_tableView registerNib:[UINib nibWithNibName:@"DiscussTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kDiscussTableViewCellReuse];
    
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [self handleDiscussData];
            
            [_tableView.mj_header endRefreshing];
            
        });
    }];
    [self.tableView.mj_header beginRefreshing];

    _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [_tableView.mj_footer endRefreshing];
        });
    }];
}

- (void)handleDiscussData{
    //    NSString *newsId = _rankData.data.newsId;
    //    NSString *str1 = @"http://211.151.51.100/v3/comment/list?messageId=";
    //    NSString *str2 = @"&messageType=NEWS&engine=&commentId=&pageIndex=0&pageSize=20&rangeType=1";
    //    NSString *urlString = [[str1 stringByAppendingString:newsId] stringByAppendingString:str2];
    NSString *urlString = @"http://211.151.51.100/v3/comment/list?messageId=cmspaa7slmotu&messageType=NEWS&engine=&commentId=&pageIndex=0&pageSize=20&rangeType=1";
    
    NSDictionary *header = @{@"User-Agent":@"Dalvik/2.1.0 (Linux; U; Android 5.0; ASUS_Z00ADB Build/LRX21V) Wuli/2.3.1 (agent:s;channel:wandoujia;credential:e1MyMDEwMDAxMDF9LXsjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIzM1Nzg3MjA2MTcxNDM4M30teyMjIyMjIyMxNDcyMDAyMTMyOTYzLTJlZjRjNThjZDJ9QDE0NzExODgxOTgzODY=;deviceId:357872061714383;osTypeId:01;detailInfo:android;simTypeId:01;netTypeId:01;deviceTypeId:01;osVersion:5.0;idfa:null)", @"Host":@"api.5wuli.com"};
    [NetworkTool GET:urlString andBody:nil andHeader:header andResponse:JHJSON andSuccessBlock:^(id result) {
        
        NSLog(@"DiscussData:%@",result);
        _discussData = [DiscussModel modelWithDic:result];
        _discussDataArray = _discussData.data.mutableCopy;
        NSLog(@"~~~~~~%ld",_discussData.data.count);
        
        [_tableView reloadData];
        

        
    } andFailureBlock:^(NSError *error) {
        
    }];
    
    
}


- (IBAction)handleBack:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (IBAction)shareButton:(id)sender {
    
    [self showAnimationView];
}
- (void)showAnimationView
{
    NSArray *imgNameArray = @[@"朋友圈"
                              ,@"微信"
                              ,@"QQ"
                              ,@"QQ空间"
                              ,@"新浪微博"
                              ,@"举报"
                              ,@"夜间模式"
                              ];
    
    NSArray *titleArray = @[@"朋友圈"
                            ,@"微信"
                            ,@"QQ"
                            ,@"QQ空间"
                            ,@"新浪微博"
                            ,@"举报",
                            @"夜间模式"
                            ];
    YYAnimationView *animationView = [[YYAnimationView alloc] initImageNameArray:imgNameArray withItmeTitleArray:titleArray];
    animationView.lineItmeNumber = 3;
    animationView.itmeSize = CGSizeMake(100, 100);
    animationView.itmeTitleFont = [UIFont systemFontOfSize:15];
    [animationView addButtonItmeToAnimationView];
    [animationView showAnimationView];
    animationView.yyAnimationButtonItmeClick = ^( NSInteger tags){
        NSLog(@"%ld",(long)tags);
    };
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _discussDataArray.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DiscussTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kDiscussTableViewCellReuse];
    NSURL *url = [NSURL URLWithString:_discussData.data[indexPath.row].portrait];
    [cell.userImage sd_setImageWithURL:url];
    cell.userName.text = _discussData.data[indexPath.row].nickName;
    cell.contentLabel.text = _discussData.data[indexPath.row].content;
    cell.timeLabel.text = [NSString stringWithFormat:@"%lld",_discussData.data[indexPath.row].createTime];
    cell.likeLabel.text = [NSString stringWithFormat:@"%ld",_discussData.data[indexPath.row].totalLikeCount];
    
    return cell;
    
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    return @"全部评论";
    
}


//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//
//    return 300;
//
//}






/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
